package ve.usb.ddp.hibernate;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import java.util.Random;


public class Usuario implements java.io.Serializable, ve.usb.cohesion.runtime.Auditable {

    /** Creates a new instance of Usuario */
    public Usuario() {}
    
    /**
     * Holds value of property id.
     */
    private Long id;

    /**
     * Getter for property id.
     * @return Value of property id.
     */
    public Long getId() {
        return this.id;
    }

    /**
     * Holds value of property idUsuario.
     */
    private long idUsuario;

    /**
     * Getter for property idUsuario.
     * @return Value of property idUsuario.
     */
    public long getIdUsuario() {
        return this.idUsuario;
    }

    /**
     * Setter for property idUsuario.
     * @param idUsuario New value of property idUsuario.
     */
    public void setIdUsuario(long idUsuario) {
        this.id = this.idUsuario = idUsuario;
    }

    /**
     * Holds value of property user_login.
     */
    private String user_login = "";

    /**
     * Getter for property user_login.
     * @return Value of property user_login.
     */
    public String getUser_login() {
        return this.user_login;
    }

    /**
     * Setter for property user_login.
     * @param user_login New value of property user_login.
     */
    public void setUser_login(String user_login) {
        this.user_login = user_login;
    }

    /**
     * Holds value of property nombreCompleto.
     */
    private String nombreCompleto = "";

    /**
     * Getter for property nombreCompleto.
     * @return Value of property nombreCompleto.
     */
    public String getNombreCompleto() {
        return this.nombreCompleto;
    }

    /**
     * Setter for property nombreCompleto.
     * @param nombreCompleto New value of property nombreCompleto.
     */
    public void setNombreCompleto(String nombreCompleto) {
        this.nombreCompleto = nombreCompleto;
    }

    /**
     * Holds value of property password.
     */
    private String password = "";

    /**
     * Getter for property password.
     * @return Value of property password.
     */
    public String getPassword() {
        return this.password;
    }

    /**
     * Setter for property password.
     * @param password New value of property password.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Holds value of property correo.
     */
    private String correo = "";

    /**
     * Getter for property correo.
     * @return Value of property correo.
     */
    public String getCorreo() {
        return this.correo;
    }

    /**
     * Setter for property correo.
     * @param correo New value of property correo.
     */
    public void setCorreo(String correo) {
        this.correo = correo;
    }

    /**
     * Holds value of property tipo.
     */
    private short tipo;

    /**
     * Getter for property tipo.
     * @return Value of property tipo.
     */
    public short getTipo() {
        return this.tipo;
    }

    /**
     * Setter for property tipo.
     * @param tipo New value of property tipo.
     */
    public void setTipo(short tipo) {
        this.tipo = tipo;
    }



    public static boolean emailValido(String email) {
        String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
        return email.matches(EMAIL_REGEX);
    }
    
    public synchronized String encryptPass(String pass) throws Exception {

        MessageDigest md = null;

        try {
        md = MessageDigest.getInstance("SHA-1");
        } catch(NoSuchAlgorithmException e) {
            throw e;
        }

        try {
            md.update(pass.getBytes("UTF-8"));
        } catch(UnsupportedEncodingException e) {
            throw e;
        }

        byte raw[] = md.digest();
        String encryptedPass = (new BASE64Encoder()).encode(raw);

        return encryptedPass;
    }

    public static String getRandomPass() {
        // Generar nuevo password
        String charSet = "0123456789abcdefghijklmnopqrstuvwxyz";
        Random rand = new Random(System.currentTimeMillis());
        String newPass = "";

        // longitud del nuevo password
        int newPassLength = 10;           
        for (int i = 0; i < newPassLength; i++) {
            int pos = rand.nextInt(charSet.length());
            newPass = newPass + charSet.charAt(pos);
        }

        return newPass;
    }

    public String toString() {
        return "<Usuario id='C" + this.id +
      //idUsuario
      "\" user_login =\"" + user_login +
      "\" nombreCompleto =\"" + nombreCompleto +
      "\" password =\"" + password +
      "\" correo =\"" + correo +
      "\" tipo =\"" + tipo +

        "'/>";
    }

}